package gjbsh.bangong.config;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
 
@Configuration
@MapperScan(basePackages = "gjbsh.bangong.ds.dao",sqlSessionFactoryRef = "dsSqlSessionFactory")
public class MybatisDsConfig {
    @Primary
    @Bean(name = "dsDataSource")
    @ConfigurationProperties("spring.datasource.ds")
    public DataSource masterDataSource(){
        return DruidDataSourceBuilder.create().build();
    }
 
    @Bean(name = "dsSqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("dsDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
        sessionFactoryBean.setDataSource(dataSource);
        sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("classpath:gjbsh/bangong/ds/mapper/*.xml"));
        return sessionFactoryBean.getObject();
    }
    
    @Bean
    public PlatformTransactionManager dsTransactionManager(@Qualifier("dsDataSource")DataSource sitDataSource) {
     return new DataSourceTransactionManager(sitDataSource);
    }
}
